package com.qqt.csr.order.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.qqt.csr.common.enums.YesOrNotEnum;
import com.qqt.csr.common.utils.SnowflakeUtil;
import com.qqt.csr.order.enums.WorkOrderStatusEnum;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.Optional;

/**
 * 人工客服工单
 *
 * @author
 */
@Getter
@Setter
@Accessors(chain = true)
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "csr_work_order")
@JsonIgnoreProperties(ignoreUnknown = true)
public class WorkOrder implements Serializable {
    @Serial
    private static final long serialVersionUID = -3582042456474692585L;
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;
    /**
     * 租户id
     */
    @TableField(value = "tenant_id")
    private String tenantId;

    /**
     * 渠道id
     */
    @TableField(value = "channel_id")
    private String channelId;

    /**
     * 工单内容
     */
    @TableField(value = "content")
    private String content;

    /**
     * 关联的会话id
     */
    @TableField(value = "conversation_id")
    private String conversationId;

    /**
     * 关联的会话类型：0 不关联；1 机器人；2 人工
     */
    @TableField(value = "conversation_type")
    private Integer conversationType;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 创建人id
     */
    @TableField(value = "creator_id")
    private Long creatorId;

    /**
     * 是否移除：0 否；1 是
     */
    @TableField(value = "delete_flag")
    @TableLogic(value = "0", delval = "1")
    private Integer deleteFlag;

    /**
     * 处理人id
     */
    @TableField(value = "designee_id")
    private Long designeeId;
    /**
     * 分配原因
     */
    @TableField(value = "allocate_cause")
    private String allocateCause;

    /**
     * 处理中的已耗时，单位秒
     */
    @TableField(value = "elapsed_time")
    private Long elapsedTime;

    /**
     * 是否重新打开：0 否；1 是
     */
    @TableField(value = "open_again_flag")
    private Integer openAgainFlag;

    /**
     * 工单编码
     */
    @TableField(value = "order_code")
    private String orderCode;

    /**
     * 工单来源：0 工单池；1 人工客服；2 h5渠道
     */
    @TableField(value = "origin")
    private Integer origin;

    /**
     * 优先级：0 低；1 一般；2 紧急
     */
    @TableField(value = "priority")
    private Integer priority;

    /**
     * 工单状态
     */
    @TableField(value = "`status`")
    private String status;

    /**
     * 超时时间，单位秒
     */
    @TableField(value = "timeout_config")
    private Long timeoutConfig;

    /**
     * 是否设置超时：0 否；1 是
     */
    @TableField(value = "timeout_flag")
    private Integer timeoutFlag;

    /**
     * 是否系统通知：0 否；1 是
     */
    @TableField(value = "sys_notify_flag")
    private Integer sysNotifyFlag;

    /**
     * 标题
     */
    @TableField(value = "title")
    private String title;

    /**
     * 更新时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 访客id
     */
    @TableField(value = "visitor_id")
    private String visitorId;

    /**
     * 初始化新工单
     *
     * @param orderCode
     * @param creatorId
     */
    @JsonIgnore
    public void init(String orderCode, Long creatorId, String tenantId) {
        this.id = SnowflakeUtil.nextId();
        this.deleteFlag = 0;
        this.orderCode = orderCode;
        this.creatorId = creatorId;
        this.elapsedTime = 0L;
        this.openAgainFlag = YesOrNotEnum.NOT.getValue();
        this.tenantId = tenantId;
        this.status = WorkOrderStatusEnum.NONE.getStatus();
        this.channelId = Optional.ofNullable(this.channelId).orElse(StringUtils.EMPTY);
        this.visitorId = Optional.ofNullable(this.visitorId).orElse(StringUtils.EMPTY);
        this.designeeId = Optional.ofNullable(this.designeeId).orElse(0L);
        this.sysNotifyFlag = Optional.ofNullable(this.sysNotifyFlag).orElse(0);
        this.timeoutConfig = Optional.ofNullable(this.timeoutConfig).orElse(0L);
    }

    /**
     * 是否需要发送系统通知
     *
     * @return
     */
    @JsonIgnore
    public boolean isNeedSysNotify() {
        return this.sysNotifyFlag.equals(YesOrNotEnum.YES.getValue());
    }

    /**
     * 是否超时配置
     *
     * @return
     */
    @JsonIgnore
    public boolean isTimeOutConfig() {
        return this.timeoutFlag.equals(YesOrNotEnum.YES.getValue());
    }

    /**
     * 是否关联会话
     *
     * @return
     */
    @JsonIgnore
    public boolean isBindConversation() {
        return !this.conversationType.equals(YesOrNotEnum.NOT.getValue());
    }

    /**
     * 是否已有处理人跟进
     *
     * @return
     */
    @JsonIgnore
    public boolean isAllocated() {
        return this.designeeId != null && this.designeeId > 0L;
    }

    /**
     * 是否待处理的工单
     *
     * @return
     */
    @JsonIgnore
    public boolean isToProcess() {
        return WorkOrderStatusEnum.TO_PROCESS.getStatus().equals(this.status);
    }

    /**
     * 是否待认领的工单
     *
     * @return
     */
    @JsonIgnore
    public boolean isToClaim() {
        return WorkOrderStatusEnum.TO_CLAIM.getStatus().equals(this.status);
    }

    /**
     * 是否挂起的工单
     *
     * @return
     */
    @JsonIgnore
    public boolean isSuspended() {
        return WorkOrderStatusEnum.SUSPENDED.getStatus().equals(this.status);
    }

    /**
     * 是否删除
     *
     * @return
     */
    @JsonIgnore
    public boolean isDeleted() {
        return this.deleteFlag != null && YesOrNotEnum.YES.getValue() == this.deleteFlag;
    }


}